//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.Research.ScientificWorkflow.NetCDF.CSharpAPI {
    public class NetCDFVariable<T> : INetCDFVariable{
        string name;
        uint[] dimensionIDs;
        Dictionary<string, INetCDFAttribute> attributes;
        NetCDFDataType dataType;
        uint size;
        int dataOffsetInFile;
        bool isRecordVariable;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aName">Variable name</param>
        /// <param name="theDimensions"></param>
        /// <param name="theAttributes"></param>
        /// <param name="offset"></param>
        public NetCDFVariable(string aName, uint[] theDimensions,
            Dictionary<string, INetCDFAttribute> theAttributes, 
            NetCDFDataType aDataType, uint theVariableSize, int theDataOffset,
            bool isARecordVariable) {

                this.name = aName;
                this.dimensionIDs = theDimensions;
                this.attributes = theAttributes;
                this.dataType = aDataType;
                this.size = theVariableSize;
                this.dataOffsetInFile = theDataOffset;
                this.isRecordVariable = isARecordVariable;
        }

        public int DataOffset
        {
            get { return this.dataOffsetInFile; }
        }

        #region INetCDFVariable Members

        public string Name
        {
            get { return this.name; }
        }

        public uint[] DimensionIDs
        {
            get { return this.dimensionIDs; }
        }

        public Dictionary<string, INetCDFAttribute> Attributes
        {
            get { return this.attributes; }
        }

        public NetCDFDataType DataType
        {
            get { return this.dataType; }
        }

        public uint Size
        {
            get { return this.size; }
        }

        public bool IsRecordVariable
        {
            get { return this.isRecordVariable; }
        }

        public uint NumValues
        {
            get
            {
                uint typeSize = sizeof(int);
                switch (this.dataType)
                {
                    case NetCDFDataType.NcByte:
                        typeSize = sizeof(byte);
                        break;
                    case NetCDFDataType.NcChar:
                        typeSize = sizeof(char);
                        break;
                    case NetCDFDataType.NcDouble:
                        typeSize = sizeof(double);
                        break;
                    case NetCDFDataType.NcFloat:
                        typeSize = sizeof(float);
                        break;
                    case NetCDFDataType.NcInt:
                        typeSize = sizeof(int);
                        break;
                    case NetCDFDataType.NcShort:
                        typeSize = sizeof(short);
                        break;
                }

                return ( this.size / typeSize);
            }
        }
        #endregion
    }
}
